package spring.ioc;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import org.testng.annotations.Test;
import spring.inters.HelloApi;
import spring.bean.setter.ListTestBean;
import spring.model.NavigationA;
import spring.model.NavigationC;

public class TestSpringDI {
    @Test
    public void testConstructorDependencyInjectTest() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/spring.ioc/constructorDependencyInject.xml");
        HelloApi byIndex = beanFactory.getBean("byIndex", HelloApi.class);
        byIndex.sayHello();
        HelloApi byType = beanFactory.getBean("byType", HelloApi.class);
        byType.sayHello();
        HelloApi byName = beanFactory.getBean("byName", HelloApi.class);
        byName.sayHello();
    }

    @Test
    public void SetterDependencyInject() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/setterDependencyInject.xml");
        HelloApi bean = beanFactory.getBean("bean", HelloApi.class);
        bean.sayHello();
    }


    @Test
    public void testListInject() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/listInject.xml");
        ListTestBean listBean = beanFactory.getBean("listBean", ListTestBean.class);
        System.out.println(listBean.getValues().size());
    }

    @Test
    public void tetBeanInject() {
        BeanFactory beanFactory = new ClassPathXmlApplicationContext("src/main/resources/beanInject.xml");
        HelloApi bean1 = beanFactory.getBean("bean1", HelloApi.class);
        bean1.sayHello();
        HelloApi bean2 = beanFactory.getBean("bean2", HelloApi.class);
        bean2.sayHello();
    }

    @Test
    public void testLocalAndparentBeanInject() {
        ApplicationContext parentBeanContext =
                new ClassPathXmlApplicationContext("src/main/resources/parentBeanInject.xml");
        ApplicationContext beanContext = new ClassPathXmlApplicationContext(new String[]{"src/main/resources/localBeanInject.xml"}, parentBeanContext);
        HelloApi bean1 = beanContext.getBean("bean1", HelloApi.class);
        bean1.sayHello();
        HelloApi bean2 = beanContext.getBean("bean2", HelloApi.class);
        bean2.sayHello();
    }

    @Test
    public void testtInnerBeanInject() {
        ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/innerBeanInject.xml");
        HelloApi bean = context.getBean("bean", HelloApi.class);
        bean.sayHello();
    }

    @Test
    public void testNavigationBeanInject() {
        ApplicationContext context = new ClassPathXmlApplicationContext("src/main/resources/navigationBeanInject.xml");
        NavigationA navigationA = context.getBean("a", NavigationA.class);
        navigationA.getNavigationB().getNavigationC().sayNavigation();
        navigationA.getNavigationB().getList().get(0).sayNavigation();
        navigationA.getNavigationB().getMap().get("key").sayNavigation();
        navigationA.getNavigationB().getArray()[0].sayNavigation();
        ((NavigationC) navigationA.getNavigationB().getProperties().get("1")).sayNavigation();
    }

}
